// JavaScript Document
var Fe = {};
Fe.body = function(){
    var W=0, H=0, SL=0, ST=0, SW=0, SH=0;
    var w=window, d=document, dd=d.documentElement;

    W=dd.clientWidth||d.body.clientWidth;
    H= w.innerHeight||dd.clientHeight||d.body.clientHeight;

    ST=d.body.scrollTop ||dd.scrollTop;
    SL=d.body.scrollLeft||dd.scrollLeft;
    SW=Math.max(d.body.scrollWidth, dd.scrollWidth ||0);
    SH=Math.max(d.body.scrollHeight,dd.scrollHeight||0, H);

    return {
        "scrollTop":ST,
        "scrollLeft":SL,
        "documentWidth":SW,
        "documentHeight":SH,
        "viewWidth":W,
        "viewHeight":H
    };
};
Fe.on = function(el, type, handler){
    if(!(el = Fe.G(el))){ return el; }
    type = type.replace(/^on/, "").toLowerCase();
    if(el.attachEvent){
        el[type + handler] = function(){
            handler.call(el, window.event);
        };
        el.attachEvent('on' + type, el[type + handler]);
    }else{
        el.addEventListener(type, handler, false);
    }
   return el;
};

Fe.un = function(el, type, handler){
    if(!(el = Fe.G(el))){ return el; }
    type = type.replace(/^on/, "").toLowerCase();
    if(el.attachEvent){
        el.detachEvent('on' + type, el[type + handler]);
        el[type + handler] = null;
    }else{
        el.removeEventListener(type, handler, false);
    }
    return el;
};

Fe.G = function(id){return document.getElementById(id);}
G = function(id){return document.getElementById(id);}
var Pop={
    isOpenNew : false ,
    ifrname:'',
    loadfunc:'',
    onOk:function() {
     }
     ,onclosed:function() {
     }
     ,create:function() {
       if (this.isOpenNew) {
         if(Fe.G('shadowDiv')) document.body.removeChild(Fe.G('shadowDiv'));
         if(Fe.G('popDiv'))  document.body.removeChild(Fe.G('popDiv'));
         if(Fe.G('shadowIframe'))  document.body.removeChild(Fe.G('shadowIframe'));
       }
         if( !G("popDiv")){
             var sha=document.createElement("div");
             sha.id="shadowDiv";
             var p=document.createElement("div");
             p.id="popDiv";
             var fr=document.createElement("iframe");
             fr.id="shadowIframe";
             fr.style.filter="alpha(opacity=0)";
             fr.style.opacity="0";
             fr.style.position="absolute";
             p.style.width=p.style.height="500px";
       var str = '';
       str += '<div class="pop_holder1">';
       str += '<div class="poptopbg"><input type="button" id="dialogBoxClose" onclick="Pop.hide();return false" title="关闭" class="pop_close"/><div id="dialogBoxTitle"></div>';
       str += '</div>';
       str += '<div id="popBody"></div>';
       str += '<div id="ifrDiv" style="height:100%"><iframe id="popIframe" name="popIframe' + this.ifrname + '" width="100%" onload="'+ this.loadfunc +'" frameborder="0" height="100%" scrolling="yes" src="about:blank"></iframe></div>';
  
  
       str += '</div>';
       str += '<div class="pop_holder2"></div>';
       p.innerHTML = str;
             document.body.insertBefore(fr,document.body.firstChild);
             document.body.insertBefore(p,document.body.firstChild);
             document.body.insertBefore(sha,document.body.firstChild);
         }
     }
     ,resize:function (event,newW,newH){
         if(!G("popDiv"))return;
        // Pop.create();
         var _body=Fe.body();
         if(Fe.isIE){_body.documentWidth -= 20;} //防止ie出滚动条
         var _top=_body.scrollTop;
         var w = _body.documentWidth|| 500;
         var h = _body.documentHeight || 500;
         var shadowDiv = G("shadowDiv");
         var popDiv = G("popDiv");
         var sf=G("shadowIframe");
         var pw=newW||parseInt(popDiv.style.width,10)||500;
         var ph=newH||parseInt(popDiv.style.height,10)||500;
         shadowDiv.style.width=w+"px";
         shadowDiv.style.height=h+"px";
         sf.style.width=(w-20)+"px";
         sf.style.height=(h-20)+"px";
         shadowDiv.style.left=shadowDiv.style.top="0px";
         sf.style.top=sf.style.left="5px";
         var divLeft=(_body.viewWidth -pw)/2;
         var divTop = (_body.viewHeight-ph)/2+_top;
         if(divLeft < 1) divLeft=_top;
         if(divTop < 1) divTop="20";
         popDiv.style.left = divLeft+"px";
         if(parent==self){
            popDiv.style.top = divTop+"px";
         }else{
            popDiv.style.top = 100+"px";
         }
            popDiv.style.width=pw+"px";
            popDiv.style.height=ph+"px";
     }
     ,hide:function() {
         Pop.onclosed();
         try{
             G("popDiv").style.display = "none";
             G("shadowDiv").style.display = "none";
             G("shadowIframe").style.width="0px";
             G("shadowIframe").style.height="0px"; 
             G("shadowIframe").style.display="none";
         }catch(uncreatepop){}

         try{G("popIframe").src="about:blank";}catch(e)
         {
             try{document.frames["popIframe"].location="about:blank";}catch(e){}
         }

         //update     
         Fe.un(window,"resize",Pop.resize);
         Fe.un(document,"onkeydown",Pop.keyListener);

     }
     ,keyListener:function(e){
         e = window.event || e ; 
         var key = e.which || e.keyCode;
         if( 27==key ){
             // ESC to close popup 

             Pop.hide();
         }
     }
     ,config:function(title,conf){
         if(title){
             G("dialogBoxTitle").innerHTML=title;
         }
         Pop.onclosed=conf.onclosed||function(){};
         if(conf.url){
             if(conf.url!=true){
                 if(conf.nocache){
                    conf.url=conf.url+(conf.url.match(/\?/)?"&":"?")+"_t="+(new Date().getTime());
                 }
                 try{G("popIframe").src=conf.url;}
                 catch(e){
                     try{document.frames["popIframe"].location=conf.url;}
                     catch(e){}
                 }
             }
             G("ifrDiv").style.display="block";
             G("popBody").style.display="none";
             G("popBody").innerHTML="";
         }
         else{
             if(conf.info){
                 G("ifrDiv").style.display="none";
                 G("popBody").style.display="block";
                 G("popBody").innerHTML=conf.info;
             }
         }
         var pw=conf.width||450;
         var ph=conf.height||400;
         var p=G("popDiv");
         p.style.width=pw+"px";
         p.style.height=ph+"px";
     }

     ,show:function (title,conf) {
         Pop.create();
         Pop.config(title,conf);
         Pop.resize();
         G("popDiv").style.display = "block";
         G("shadowDiv").style.display = "block";
         G("shadowIframe").style.display="";
         Fe.on(window,"resize",Pop.resize);
         Fe.on(document,"onkeydown",Pop.keyListener);

     }
     ,confirm:function(msg,conf) {
         Pop.onOk=proxy(function(func){Pop.onclosed=function(){};Pop.hide();func();},conf.ok||Pop.hide);
         var m="<span class='f14'>$2$</span><br><br><p align='center'><input type='button'value='$0$' onclick='Pop.onOk();'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='button'value='$1$' onclick='Pop.hide()'></p>";
         m=m.format(conf.okInfo||"确定",conf.cancelInfo||"取消",msg);
         Pop.show(conf.title||"信息确认",{info:m,width:(conf.width||300),height:(conf.height||200),onclosed:conf.cancel});
     }  
     ,setTitle:function(title){
        G("dialogBoxTitle").innerHTML=title;
     }
};